package com.beilang.hotline.sys.page;

import java.util.Date;
import java.util.List;

import com.beilang.base.core.page.BasePage;
import com.beilang.base.exception.CheckException;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.property.AppDefaultProperty;
import com.beilang.base.util.DateUtil;
import com.beilang.base.util.CodeConstant;
import com.beilang.hotline.sys.manager.SysMgr;
import com.beilang.hotline.sys.model.SysModel;
import com.beilang.hotline.sys.to.SearchTO;
import com.beilang.hotline.user.model.UserModel;


public class SysPage extends BasePage {
	private SysMgr sysMgr;

	public void setSysMgr(SysMgr sysMgr) {
		this.sysMgr = sysMgr;
	}

	public PaginationHelper getSearchList(PaginationHelper helper,SearchTO searchTO) {
		helper = sysMgr.getSysList(helper, searchTO);
		return helper;
	}

	public PaginationHelper deleteSysList(PaginationHelper helper,SearchTO searchTO, List deleteList) {
        for (Object obj : deleteList) {
            SysModel sysModel = (SysModel) obj;
            sysMgr.deleteSys(sysModel.getSysId());
        }
		return this.getSearchList(helper, searchTO);
	}

	public SysModel addSys(SysModel sysModel) throws CheckException {
		UserModel user=this.getLoginUser();
		List list=sysMgr.getAllSys();
		if(list!=null&&list.size()>0){
            for (Object obj : list) {
                SysModel s = (SysModel) obj;
                if (s.getSysName().equalsIgnoreCase(sysModel.getSysName())) {
                    throw new CheckException(AppDefaultProperty.getProperty(AppDefaultProperty.getProperty(CodeConstant.ERROR_RECORD_ALREADY_EXIST)));
                }
            }
		}
		sysModel.setCreateId(user.getUserId());
		sysModel.setUpdateId(user.getUserId());
		Date date = DateUtil.getDate();
		sysModel.setCreateTime(date);
		sysModel.setUpdateTime(date);
		sysModel = sysMgr.createSys(sysModel);
		return sysModel;
	}

	public SysModel getViewSys(Long sysId) {
		return sysMgr.getSys(sysId);
	}
	
	public SysModel getUpdateSys(Long sysId) {
		return this.getViewSys(sysId);
	}	

	public SysModel updateSys(SysModel sysModel) throws CheckException{
		List list=sysMgr.getAllSys();
		if(list!=null&&list.size()>0){
            for (Object obj : list) {
                SysModel s = (SysModel) obj;
                if (s.getSysName().equalsIgnoreCase(sysModel.getSysName())) {
                    if (s.getSysId().longValue() != sysModel.getSysId().longValue())
                        throw new CheckException(AppDefaultProperty.getProperty(CodeConstant.ERROR_RECORD_ALREADY_EXIST));
                }
            }
		}		
		UserModel user=this.getLoginUser();
		sysModel.setUpdateId(user.getUserId());
		sysModel.setUpdateTime(DateUtil.getDate());		
		sysModel = sysMgr.saveSys(sysModel);
		return sysModel;
	}

}
